package com.lsl.endworkvol.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lsl.endworkvol.dao.ActivityMapper;
import com.lsl.endworkvol.dao.SignUpNoteMapper;
import com.lsl.endworkvol.entity.Activity;
import com.lsl.endworkvol.entity.SignUpNote;
import com.lsl.endworkvol.service.ActivityService;
import com.lsl.endworkvol.service.func.IActivityBulider;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private SignUpNoteMapper signUpNoteMapper;

    @Override
    public boolean createActivity(IActivityBulider bulider) {
        return activityMapper.insert(bulider.build()) > 0;
    }

    @Override
    public boolean deleteActivity(String id) {
        // System.out.println("[ActivityServiceImpl] +++++ 删除活动开始：" + id);
        // System.out.println("[ActivityServiceImpl] +++++ 正在删除该活动的报名记录：" + id);
        List<SignUpNote> notes = signUpNoteMapper.selectByActivityId(Integer.parseInt(id));
        notes.forEach(note -> {
            signUpNoteMapper.delete(note.getId());
        });
        // System.out.println("[ActivityServiceImpl] +++++ 正在删除该活动：" + id);
        return activityMapper.delete(Integer.parseInt(id)) > 0;
    }

    @Override
    public boolean editActivity(IActivityBulider bulider) {
        return activityMapper.update(bulider.build()) > 0;
    }

    /*
     * 重载（实现） getActivities 方法，根据 Activity 实体类查询
     */
    @Override
    public List<Activity> getActivities(IActivityBulider bulider) {
        return activityMapper.blur(bulider.build());
    }

    /*
     * 重写 getActivities 方法，根据leaderId查询
     */
    @Override
    public List<Activity> getActivities(String leaderId) {
        return activityMapper.selectByLeaderId(Integer.parseInt(leaderId));
    }

    /*
     * 重写 getActivities 方法，查询所有
     */
    @Override
    public List<Activity> getActivities() {
        return activityMapper.selectAll();
    }

    @Override
    public Activity getActivity(String id) {
        return activityMapper.selectById(Integer.parseInt(id));
    }

}
